package cn.edu.zjgsu.ito.controller;

import cn.edu.zjgsu.ito.interceptor.TeacherControllerInterceptor;
import cn.edu.zjgsu.ito.service.ServiceFactory;
import cn.edu.zjgsu.ito.service.TeaInfService;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;

@Before(TeacherControllerInterceptor.class)
public class TeaInfController extends Controller {
	/*
	 * 张虎涛
	 * 2016/06/01
	 */
	private TeaInfService teaInfoService =ServiceFactory.createTeaInfService();
	private String tno;
	private Record record;
	
	private boolean TF1;
	private boolean TF2;
	private String message;
	
	private String oldPassword;
	private String newPassword;
	private String rePassword;
	
	
	/**
	 * 教师用户查看自身信息
	 */
	public void index(){
		try{
			if(getSessionAttr("tNo")==null){
				System.out.println("登录超时，请从新登陆!");
				render("../common/login/login.html");
				return;
			}else{
				tno=getSessionAttr("tNo");
			}
			record=teaInfoService.search(tno);
			
			if(record==null){
				redirect("/toErrors");
				return;
			}
			else{
				setAttr("record",record);
				render("tinformation.html");
				return;
			}	
		}
		catch(Exception e){
			
		}					
	}
	
	/**
	 * 教师用户查看自身信息
	 */
	public void toUpdateInf(){
		try{
			if(getSessionAttr("tNo")==null){
				System.out.println("登录超时，请从新登陆!");
				render("../common/login/login.html");
			}else{
				tno=getSessionAttr("tNo");
			}
			record=teaInfoService.search(tno);
			
			if(record==null){
				redirect("/toErrors");
				return;
			}
			else{
				setAttr("record",record);
				
				render("correcttinfo.html");
				return;
			}	
		}
		catch(Exception e){
			
		}					
	}
	
	/**
	 * 教师用户修改自身信息
	 */
	public void update(){
		TF1=false;//初始化
		TF2=false;//初始化
		message="";//初始化		
		
		oldPassword=getPara("oldPassword");
		newPassword=getPara("newPassword");
		rePassword=getPara("rePassword");
		
		try{
			if(getSessionAttr("tNo")==null){
				System.out.println("登录超时，请从新登陆!");	
			}
			else{
				tno=getSessionAttr("tNo");

				if((oldPassword==null||oldPassword.equals("")) ){
					setAttr("message","原始密码不能为空");
					toUpdateInf();
					return;
				}
				if(newPassword==null||newPassword.equals("")||rePassword==null||rePassword.equals("")){
					System.out.println("message新密码不能为空");
					setAttr("message","新密码不能为空");
					toUpdateInf();
					return;
				}

				if(!newPassword.equals(rePassword)){
					setAttr("message","两次密码不一致");
					toUpdateInf();
					return;
				}
			}						
			
			TF1=teaInfoService.checkPassword(tno, oldPassword);
			if(TF1){
				TF2=teaInfoService.updatePassword( newPassword,tno);
			}else{
				message+="原密码错误";
				index();
			}
			if(TF2){
				redirect("/upPass");
				return;
			}else{
				message+="密码修改失败错误";
			}
						
			setAttr("message",message);
			toUpdateInf();
			return;
		}
		catch(Exception e){
			
		}
	}

}
